//
//  XJComposeView.m
//  weiboOC
//
//  Created by 徐绩 on 16/4/28.
//  Copyright © 2016年 ayumi. All rights reserved.
//

#import "XJComposeView.h"
#import "XJComposeMenuItemsModel.h"
#import "XJComposeBtn.h"
#import "XJComposeController.h"

@interface XJComposeView ()

@property (nonatomic,strong) NSArray *itemsArr;
@property (nonatomic,strong) XJComposeMenuItemsModel *itemModel;
@property (nonatomic,strong) NSMutableArray *btnArr;
@property (nonatomic,strong) UIViewController *target;

@end


@implementation XJComposeView

-(NSMutableArray *)btnArr{

    if (_btnArr == nil) {
        
        NSMutableArray *arrM = [NSMutableArray array];
        _btnArr = arrM;
    }
    return _btnArr;
}

/**
 *  懒加载数据
 */
-(NSArray *)itemsArr{
    
    if (_itemsArr == nil) {
        
        NSString *path = [[NSBundle mainBundle]pathForResource:@"compose.plist" ofType:nil];
        
        NSArray *tempArray = [NSArray arrayWithContentsOfFile:path];
        
        NSMutableArray *mutable = [NSMutableArray array];
        for (NSDictionary *dict in tempArray) {
            
            XJComposeMenuItemsModel *model = [XJComposeMenuItemsModel composeMenuItemsModelWithDict:dict];
            [mutable addObject:model];
        }
        _itemsArr = mutable;
    }
    return _itemsArr;
}

-(instancetype)initWithFrame:(CGRect)frame{

    if (self = [super initWithFrame:frame]) {
        
        [self setupUI];
    }
    return self;
}

-(void)setupUI{
    
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    CGRect frame = window.frame;
    
    //开启图形上下文
    UIGraphicsBeginImageContextWithOptions(frame.size, false, 1);
    
    //将内容渲染到图形上下文
    [window drawViewHierarchyInRect:frame afterScreenUpdates:YES];
    
    //获取图片
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    
    //关闭上下文
    UIGraphicsEndImageContext();
    
    //磨砂效果
   UIImage *image = [img applyLightEffect];
 
    UIImageView *imageView = [[UIImageView alloc]initWithImage:image];
    [self addSubview:imageView];
    
    //添加标语
    UIImageView *sloganImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"compose_slogan"]];
    [self addSubview:sloganImage];
    
    [sloganImage makeConstraints:^(MASConstraintMaker *make) {
       
        make.centerX.equalTo(self);
        make.top.equalTo(150);
    }];
    
    [self addMenuBtns];
}

/**
 *  添加菜单按钮
 */
-(void)addMenuBtns{

    CGFloat itemW = 80;
    CGFloat itemH = 110;
    CGFloat kMargin = (screenW - 3 * itemW)/4;
    
    for (int i = 0; i < self.itemsArr.count; i++) {
        
        int col = i % 3;
        int row = i / 3;
        CGFloat itemX = kMargin * (col + 1) + col * itemW;
        CGFloat itemY = row * 10 + row * itemH + screenH;
        
        XJComposeBtn *btn = [[XJComposeBtn alloc]initWithFrame:CGRectMake(itemX, itemY, itemW, itemH)];
        btn.tag = i;
        
        [btn addTarget:self action:@selector(didClickComposeBtn:) forControlEvents:UIControlEventTouchUpInside];
        
        btn.model = self.itemsArr[i];
        [self.btnArr addObject:btn];
        
        [self addSubview:btn];

    }
}

/**
 供外界调用弹出控制器
 */
-(void)show:(UIViewController *)target{
    [target.view addSubview:self];
    self.target = target;

}


/**
 *  composeBtn的点击事件
 */
-(void)didClickComposeBtn:(UIButton *)button{
    
    [UIView animateWithDuration:0.5 animations:^{
        
        for (XJComposeBtn *btn in self.btnArr) {
            
            btn.alpha = 0.01;
            if (btn == button) {
                
                btn.transform = CGAffineTransformMakeScale(2, 2);
            }else{
                
                btn.transform = CGAffineTransformMakeScale(0.01, 0.01);
            }
        }
    } completion:^(BOOL finished) {
//        
//        XJComposeMenuItemsModel *model = self.itemsArr[button.tag];
//        Class TargetVC = NSClassFromString(model.className);
//        
//        UIViewController *targetVC = [[TargetVC alloc]init];
//        
//        XJComposeController *composeVC = (XJComposeController *)targetVC;
        XJComposeController *composeVC = [[XJComposeController alloc]init];
        UINavigationController *navTarget = [[UINavigationController alloc]initWithRootViewController:composeVC];
        
        [self.target presentViewController:navTarget animated:YES completion:^{
            [self removeFromSuperview];
        }];
        
    }];
    
}



-(void)willMoveToSuperview:(UIView *)newSuperview{

    [super willMoveToSuperview:newSuperview];
    
    for (int i = 0; i<self.btnArr.count; i++) {
        
        UIButton *btn = self.btnArr[i];
        
        [self animation:btn index:i isUp:YES];
    }
}

/**
 *  修改btn位置
 */
-(void)animation:(UIView *)view index:(int)index isUp:(BOOL)isUp{

   POPSpringAnimation *ani = [POPSpringAnimation animationWithPropertyNamed:kPOPViewCenter];
    CGPoint point = CGPointMake(view.center.x, view.center.y + (isUp ? -370 : 370));
    ani.toValue = [NSValue valueWithCGPoint:point];
    
    ani.springSpeed = 10;
    ani.springBounciness = 8;
    ani.beginTime = index * 0.025 + CACurrentMediaTime();
    
    [view pop_addAnimation:ani forKey:nil];


}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

    [super touchesBegan:touches withEvent:event];
    
    NSEnumerator *enumer = self.btnArr.reverseObjectEnumerator;
    NSArray *arr = enumer.allObjects;

    for (int i = 0; i<arr.count; i++) {
        
        UIButton *btn = arr[i];
        
        [self animation:btn index:i isUp:NO];
    }
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self removeFromSuperview ];
    });
}
 

@end
